package io.noties.markwon.base.core.spans

import android.text.TextPaint
import android.text.style.URLSpan
import android.view.View
import io.noties.markwon.base.LinkResolver
import io.noties.markwon.theme.MarkwonTheme

class LinkSpan(
    private val theme: MarkwonTheme,
    /**
     * @since 4.2.0
     */
    val link: String,
    private val resolver: LinkResolver
) : URLSpan(link) {
    override fun onClick(widget: View) {
        resolver.resolve(widget, link)
    }

    override fun updateDrawState(ds: TextPaint) {
        applyLinkStyle(ds)
    }

    private fun applyLinkStyle(paint: TextPaint) {
        paint.isUnderlineText = theme.typography.isLinkUnderlined
        if (theme.colorTheme.linkColor != 0L) {
            paint.color = theme.colorTheme.linkColor.toInt()
        } else {
            // if linkColor is not specified during configuration -> use default one
            paint.color = paint.linkColor
        }
    }
}